/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.eschool.dao;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import com.eschool.model.Permission;
import com.eschool.model.Role;
import com.eschool.model.SaasAdmin;
import com.eschool.util.HibernateUtil;
import com.eschool.util.JdbcUtil;

/**
 *
 * @author Ammar
 */

public class SaasAdminDao{
	
	
	public static boolean addAdmin(SaasAdmin admin) {
    	
    	boolean isInserted = false;
    	
    	Connection connection = JdbcUtil.openConnection();
		
		try{
			CallableStatement storedProc = connection.prepareCall("{ call InsertSaasAdmin(?,?,?,?,?,?,?) }");
			
			storedProc.setString(1, admin.getName());
			storedProc.setString(2, admin.getPhoneNumber());
			storedProc.setString(3, admin.getMobileNumber());
			storedProc.setString(4, admin.getAddress());
			storedProc.setString(5, admin.getEmail());
			storedProc.setString(6, admin.getPassword());
			storedProc.setInt(7, admin.getRole().getRoleId());
			//System.out.println(admin.getRole().getRoleId());
			storedProc.execute();
		
			isInserted = true;
			connection.close();
		}
		
		catch(Exception e){
			isInserted = false;
			connection.close();
			e.printStackTrace();
		}
		finally{
			return isInserted;
		}
	}

	
	public static SaasAdmin getAdminByProperty(String property, Object value) {
		
		Session session = HibernateUtil.currentSession();
	      
		Criteria crit = session.createCriteria(SaasAdmin.class);
		crit.add(Restrictions.eq(property, value));
		
		SaasAdmin admin= (SaasAdmin) crit.uniqueResult();

        HibernateUtil.closeSession(session);

        return admin;
	}

	
	public static List<SaasAdmin> getAllSaasAdmin() {
		Session session = HibernateUtil.currentSession();
	      
		Criteria crit = session.createCriteria(SaasAdmin.class);
		
		List admins = crit.list();
		
        HibernateUtil.closeSession(session);
        return admins;
	}
}
